#include "dlgtenantedit.h"
#include "ui_dlgtenantedit.h"


DlgTenantEdit::DlgTenantEdit(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgTenantEdit)
{
    ui->setupUi(this);
    connect(ui->btn_ok, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->btn_cancel, SIGNAL(clicked()), this, SLOT(reject()));
}

DlgTenantEdit::~DlgTenantEdit()
{
    delete ui;

}


void DlgTenantEdit::setup_connection()
{
    connect(ui->ledit_family, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_name, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_passdep, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_passnum, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_pname, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->ledit_regadr, SIGNAL(textChanged(QString)), this, SLOT(data_changed()));
    connect(ui->dedit_dob, SIGNAL(dateChanged(QDate)), this, SLOT(data_changed()));
    connect(ui->dedit_passdate, SIGNAL(dateChanged(QDate)), this, SLOT(data_changed()));
    connect(ui->tedit_comments, SIGNAL(textChanged()), this, SLOT(data_changed()));
}

void DlgTenantEdit::data_to_form()
{
    if(mqh_data.isEmpty()) return;
    ui->ledit_family->setText( mqh_data.value(FLD_TEN_FAM).toString());
    ui->ledit_name->setText( mqh_data.value(FLD_TEN_NAME).toString());
    ui->ledit_pname->setText(mqh_data.value(FLD_TEN_PNAME).toString());
    ui->dedit_dob->setDate(mqh_data.value(FLD_TEN_DOB).toDate());

    ui->ledit_passnum->setText(mqh_data.value(FLD_TEN_PASSNUM).toString());
    ui->ledit_passdep->setText(mqh_data.value(FLD_TEN_PASSDEP).toString());
    ui->ledit_regadr->setText(mqh_data.value(FLD_TEN_REGADR).toString());
    ui->tedit_comments->setPlainText(mqh_data.value(FLD_TEN_COMM).toString());
    ui->dedit_passdate->setDate(mqh_data.value(FLD_TEN_PASSDATE).toDate());

}

void DlgTenantEdit::form_to_data()
{
    mqh_data[FLD_TEN_PASSNUM] = ui->ledit_passnum->text();
    mqh_data[FLD_TEN_DOB] = ui->dedit_dob->date();
    mqh_data[FLD_TEN_PASSDATE] = ui->dedit_passdate->date();
    mqh_data[FLD_TEN_FAM] = ui->ledit_family->text().toUpper().trimmed();
    mqh_data[FLD_TEN_PASSDEP] = ui->ledit_passdep->text();
    mqh_data[FLD_TEN_PNAME] = ui->ledit_pname->text().toUpper().trimmed();
    mqh_data[FLD_TEN_COMM] = ui->tedit_comments->toPlainText();
    mqh_data[FLD_TEN_NAME] = ui->ledit_name->text().toUpper().trimmed();
    mqh_data[FLD_TEN_REGADR] = ui->ledit_regadr->text();
}


void DlgTenantEdit::accept()
{
    if(ui->ledit_passnum->text().size() < 7)
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указан номер паспорта.");
        return;
    }

    if(ui->ledit_family->text().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указана фамилия.");
        return;
    }

    if(ui->ledit_name->text().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указано имя.");
        return;
    }
    if(ui->ledit_regadr->text().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указан адрес регистрации.");
        return;
    }

    if(ui->ledit_passdep->text().isEmpty())
    {
        lakhMsgE()<<RUSTR("ОШИБКА! Не указан орган, выдавший паспорт.");
        return;
    }

    QDialog::accept();
}
